package com.platinadesign.axa.view
{
	import com.platinadesign.axa.vo.Resource;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * // for this class as a view,we should deploy design pattern HOLIWOOD schema,which means
	 * this class dont know pagesManager exist which does know this class to order view to excute logic
	 * @author DuMing
	 *
	 */
	public class SystemViewContainer extends Sprite
	{

		public function get pageContainer() : Sprite
		{
			return _pageContainer;
		}

		public function get background() : BackgroundView
		{
			return _background;
		}

		public function get controlBar() : BarnerView
		{
			return _controlBar;
		}

		public function get turningPage() : TurningPages
		{
			return _turningPage;
		}

		private var _pageContainer : Sprite;

		private var _turningPage : TurningPages;

		private var _controlBar : BarnerView;

		private var _background : BackgroundView;

		private var _pages : Vector.<Page>;

		private var _leftCorner : PageCorner;

		private var _rightCorner : PageCorner;

		/**
		 * 构造期间可以创建实例，排列整齐是必须的，这样一眼就看的清楚，记住了
		 *
		 */
		public function SystemViewContainer()
		{
			_background = new BackgroundView();
			_pageContainer = new Sprite();
			_turningPage = new TurningPages();
			_controlBar = new BarnerView();

			_leftCorner = new PageCorner( new Corner(), PageCorner.LEFT );
			_rightCorner = new PageCorner( new Corner(), PageCorner.RIGHT );

			this.addChild( _background );
			this.addChild( _pageContainer );
			this.addChild( _controlBar );
		}

		/**
		 * 开始翻页，做好准备工作，并提交翻页序列数据对象给相应实例
		 * @param direction
		 * @param overPages
		 *
		 */
		public function startTurning( direction : String, overPages : Vector.<Page> ) : void
		{
			_leftCorner.visible = _rightCorner.visible = false;
			_turningPage.visible = true;
			_turningPage.startTurning( direction, overPages );
		}

		/**
		 * 页面反转完毕后，的收尾工作
		 * @param endPageIndex
		 *
		 */
		public function stopTurning( endPageIndex : uint ) : void
		{
			_leftCorner.visible = _rightCorner.visible = true;
			_turningPage.visible = false;
			showPage( endPageIndex );
			layoutCorners( endPageIndex );
			playContent( _pages[ endPageIndex ]);
		}

		/**
		 * 根据内容列表，初始化显示对象，排列显示列表
		 * @param resource
		 *
		 */
		public function init( resource : Resource ) : void
		{
			_pages = resource.pages;

			for each( var page : Page in _pages )
			{
				_pageContainer.addChild( page );
				page.x = -page.width * 0.5
			}
			_pageContainer.addChild( _turningPage );
			_pageContainer.addChild( _leftCorner );
			_pageContainer.addChild( _rightCorner );

			_turningPage.visible = false;
			showFirstPageInitially()
		}

		// except this class, nobody has authority to operate page,so to expose api to out side is nessisary!!!
		public function hidePages( ) : void
		{
			for( var i : int = 0; i < _pages.length; i++ )
			{
				const page : Page = _pages[ i ];
				page.visible = false;
			}
		}

		/**
		 * 同一时间只会显示一个页，所以隐藏其他的
		 * @param index
		 * @return
		 *
		 */
		// sometimes its quite convenient to return a instance in which operated.
		public function showPage( index : uint ) : Page
		{
			for( var i : int = 0; i < _pages.length; i++ )
			{
				const page : Page = _pages[ i ];
				page.visible = ( i == index );
			}
			return _pages[ index ];
		}

		/**
		 * 页面转换完毕后，播放内容影片
		 * @param page
		 *
		 */
		private function playContent( page : Page ) : void
		{
			page.playContent();
		}

		/**
		 * 初始化
		 *
		 */
		private function showFirstPageInitially() : void
		{
			showPage( 0 );
			layoutCorners( 0 );
			playContent( _pages[ 0 ]);
		}

		/**
		 * 更新页脚位置，以防止页面比例一直，但是大小不一致，另外检测第一页和左右一页的页脚隐藏
		 * @param forPageIndex
		 *
		 */
		private function layoutCorners( forPageIndex : int ) : void
		{
			_leftCorner.x = _pages[ forPageIndex ].x;
			_rightCorner.x = _pages[ forPageIndex ].width * 0.5;

			if( forPageIndex == 0 )
			{
				_leftCorner.visible = false;
			}
			else if( forPageIndex == _pages.length - 1 )
			{
				_rightCorner.visible = false;
			}
			else
			{
				_leftCorner.visible = _rightCorner.visible = true;
			}
		}
		//看到没，即便我使用这么多空行，这么多注释，这个核心类都没超过200行，我写的程序很多年没超过300行了
	}
}